package main

import (
	"fmt"
	"nh-poc/pkg/middleware"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	// 示例 AK/SK 存储（实际应该使用数据库）
	akskStore := map[string]string{
		"ak": "sk",
	}

	// 配置中间件
	config := middleware.DefaultConfig(func(accessKey string) (string, error) {
		secretKey, ok := akskStore[accessKey]
		if !ok {
			return "", fmt.Errorf("access key not found")
		}
		return secretKey, nil
	})

	// 应用中间件
	r.Use(middleware.AKSKMiddleware(config))

	// 测试路由
	r.GET("/test", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "You have access to protected resource!",
		})
	})

	//r.Run(":8080")

	// 生成自签名证书 (on linux)
	// openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost"

	// 启动 HTTPS 服务
	r.RunTLS(":9093", "conf/certs/cert.pem", "conf/certs/key.pem")
}
